package org.unit.testmgmt.ui.commands;

import java.lang.annotation.Inherited;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.unit.testmgmt.Activator;
import org.unit.testmgmt.base.UTMController;
import org.unit.testmgmt.base.UTMException;
import org.unit.testmgmt.base.UTMWarning;
import org.unit.testmgmt.messages.Messages;

/**
 * Command for the generation of a test-class. The standard shortcut is ctrl-shift-<.
 * 
 * @author Robert Streng
 * 
 */
public class UTMCmdGenerateUnitTestclass implements IHandler {
	private static ILog log = Activator.getLogger();
	private static String pluginId = Activator.PLUGIN_ID;
	
	/**
	 * {@link Inherited}
	 */
	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {

	}

	/**
	 * {@link Inherited}
	 */
	@Override
	public void dispose() {
	}

	/**
	 * {@link Inherited}
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		
		UTMController ctrl = new UTMController();
		IWorkbenchWindow activeWorkbenchWindow;
		activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

		ISelection selection = activeWorkbenchWindow.getSelectionService().getSelection();
		boolean result = false;
		
		try {
			if (selection instanceof IStructuredSelection) {
				result = ctrl.generateTestclass(activeWorkbenchWindow,
						(IStructuredSelection) selection);

			}
			else {
				IEditorInput editorInput = activeWorkbenchWindow.getActivePage().getActiveEditor()
						.getEditorInput();
				if (editorInput instanceof IFileEditorInput) {
					result = ctrl.generateTestclass(activeWorkbenchWindow,
							((IFileEditorInput) editorInput));
				}
			}
		}
		catch (UTMException e) {
			handleError(activeWorkbenchWindow.getShell(), e);
		}
		catch (UTMWarning e) {
			String warning = Messages.General_warning;
			MessageDialog.openWarning(activeWorkbenchWindow.getShell(), warning, e.getMessage());
		}
		catch (Exception e) {
			handleError(activeWorkbenchWindow.getShell(), e);
			return null;
		}

		if (result) {
			String information = Messages.General_information;
			MessageDialog.openInformation(activeWorkbenchWindow.getShell(), information,
					Messages.General_info_generation_successful);
		}

		return null;
	}

	private void handleError(Shell shell, Exception e) {
		String error = Messages.General_error;
		String errorMsg = Messages.General_error_processing;

		IStatus status = new Status(Status.ERROR, pluginId, e.getMessage(), e);
		log.log(status);
		MessageDialog.openError(shell, error, errorMsg);
	}

	/**
	 * {@link Inherited}
	 */
	@Override
	public boolean isEnabled() {
		return true;
	}

	/**
	 * {@link Inherited}
	 */
	@Override
	public boolean isHandled() {
		return true;
	}

	/**
	 * {@link Inherited}
	 */
	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {

	}

}
